package com.tanhua.dubbo.server.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.mapper.UserMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: UserInfoService
 * @Author:王晨晗
 * @Description:
 * @Date: 2022/01/07/14:23
 */
@Service
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;




    @Autowired
    private UserMapper userMapper;

    /**
     * @description 修改用户详细信息
     * @param userInfo
     * @return
     */
    public Boolean updateUserInfoByUserId(UserInfo userInfo) {
        QueryWrapper<UserInfo> wrappr = new QueryWrapper<>();
        wrappr.eq("user_id", userInfo.getUserId());
        return userInfoMapper.update(userInfo, wrappr) > 0;
    }

    //根据id获取user信息
    public User queryUserById(Long userId){
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,userId);
        return userMapper.selectOne(wrapper);
    }

    /**
     * @description 根据用户id 获取用户信息
     * @param userId
     * @return
     */
    public UserInfo queryUserInfoByUserId(Long userId){



        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id" ,userId);
        return userInfoMapper.selectOne(wrapper);
    }

    /**
     * @description 根据条件查询用户详细信息列表
     * @param wrapper
     * @return
     */
    public List<UserInfo> queryUserInfoList(QueryWrapper<UserInfo> wrapper) {
        return userInfoMapper.selectList(wrapper);
    }

    //查看所有的userinfo列表
    public IPage<UserInfo> findAll(Integer page,Integer pagesize) {
        IPage iPage=new Page(page,pagesize);
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNotNull(UserInfo::getUserId);
        return userInfoMapper.selectPage(iPage, wrapper);
    }

    //根据城市获取userinfo
    public IPage<UserInfo> queryUserInfoByCity(String city,Integer page,Integer pagesize) {
        IPage iPage=new Page(page,pagesize);
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(UserInfo::getCity,city);
        return userInfoMapper.selectPage(iPage,wrapper);
    }

    //根据昵称获取userinfo
    public IPage<UserInfo> queryUserInfoByNickName(String nickname, Integer page, Integer pagesize) {
        IPage iPage=new Page(page,pagesize);
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(UserInfo::getNickName,nickname);
        return userInfoMapper.selectPage(iPage,wrapper);
    }
}
